package com.example.xd.defaultcall;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.xd.defaultcall.key_board.DefaultKeyBoard;

import static com.example.xd.defaultcall.Constance.CALL_BR_URL;
import static com.example.xd.defaultcall.Constance.test_url;

public class CallUIActivity extends AppCompatActivity implements View.OnClickListener, MediaPlayer.OnPreparedListener {

    private TextView mTv_call_phoneNum;
    private PhoneCallManager mPhoneCallManager;
    private CallPhoneStateBr mCallPhoneStateBr;
    private TextView mTv_call_time;
    private boolean isUsed = false;
    private boolean isClose = false;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    time++;
                    mTv_call_time.setText("通话时长:" + TimeUtils.stringForTime(time));
                    Message message = Message.obtain();
                    message.what = 1;
                    mHandler.sendMessageDelayed(message, 1000);
                    break;
            }
        }
    };
    long time = 0;
    private MyVideoView mCall_myvv;
    private ImageView mCall_volum_img;
    private boolean isRecord = false;
    private ImageView mCall_record_img;
    private DefaultKeyBoard mDefault_key_board;
    private RelativeLayout mRl_function;
    private TextView mTv_hide_keyboard;

    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_callui);

        mTv_call_phoneNum = findViewById(R.id.tv_call_phoneNum);
        findViewById(R.id.img_cut).setOnClickListener(this);
        mTv_call_time = findViewById(R.id.tv_call_time);
        mCall_myvv = findViewById(R.id.call_myvv);
        mCall_volum_img = findViewById(R.id.call_volum_img);
        mCall_record_img = findViewById(R.id.call_record_img);
        findViewById(R.id.img_edit).setOnClickListener(this);
        findViewById(R.id.img_key_board).setOnClickListener(this);
        mCall_volum_img.setOnClickListener(this);
        mCall_record_img.setOnClickListener(this);

        mRl_function = findViewById(R.id.rl_function);
        mDefault_key_board = findViewById(R.id.default_key_board);
        mTv_hide_keyboard = findViewById(R.id.tv_hide_keyboard);
        mTv_hide_keyboard.setOnClickListener(this);

        mCall_myvv.setVideoPath(test_url);
        mCall_myvv.setOnPreparedListener(this);

        MyApp application = (MyApp) getApplication();
        application.setActivity(this);
        String tel = getIntent().getStringExtra("tel");
        mTv_call_phoneNum.setText(tel);
        mPhoneCallManager = new PhoneCallManager(this);

        mCallPhoneStateBr = new CallPhoneStateBr();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(CALL_BR_URL);
        registerReceiver(mCallPhoneStateBr, intentFilter);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_cut:
                if (!isUsed) {
                    mPhoneCallManager.disconnect();
                    mPhoneCallManager.destroy();
                    isUsed = true;
                }
                if (!isClose) {
                    mCall_myvv.stopPlayback();
                    isClose = true;
                }
                break;
            case R.id.call_volum_img:
                mPhoneCallManager.setSpeakMode();
                break;
            case R.id.call_record_img:
                isRecord = !isRecord;
                if (isRecord) {
                    startService(new Intent(CallUIActivity.this, RecordingService.class));
                } else {
                    stopService(new Intent(CallUIActivity.this, RecordingService.class));
                }
                break;
            case R.id.img_edit:
                startActivity(new Intent(CallUIActivity.this, EditActivity.class));
                break;
            case R.id.img_key_board:
                if (mRl_function.getVisibility() == View.VISIBLE) {
                    mRl_function.setVisibility(View.INVISIBLE);
                    mDefault_key_board.setVisibility(View.VISIBLE);
                } else {
                    mDefault_key_board.setVisibility(View.INVISIBLE);
                    mRl_function.setVisibility(View.VISIBLE);
                }
                mTv_hide_keyboard.setVisibility(View.VISIBLE);
                break;
            case R.id.tv_hide_keyboard:
                if (mDefault_key_board.getVisibility() == View.VISIBLE) {
                    mRl_function.setVisibility(View.VISIBLE);
                    mDefault_key_board.setVisibility(View.INVISIBLE);
                } else {
                    mDefault_key_board.setVisibility(View.VISIBLE);
                    mRl_function.setVisibility(View.INVISIBLE);
                }
                mTv_hide_keyboard.setVisibility(View.GONE);

                break;
        }
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.start();
    }

    public class CallPhoneStateBr extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            String state = intent.getStringExtra("state");
            if (action == CALL_BR_URL) {
                switch (state) {
                    case "STATE_ACTIVE"://拨打电话,对方接通时的回调
                        mPhoneCallManager.answer();
                        Message msg = Message.obtain();
                        msg.what = 1;
                        mHandler.sendMessage(msg);
                        if (!isClose) {
                            mCall_myvv.stopPlayback();
                            isClose = true;
                        }
                        break;
                    case "STATE_DISCONNECTED"://拨打电话,对方挂断
                        if (!isUsed) {
                            mPhoneCallManager.disconnect();
                            mPhoneCallManager.destroy();
                            unregisterReceiver(mCallPhoneStateBr);
                            isUsed = true;
                        }
                        if (mHandler != null) {
                            mHandler.removeCallbacksAndMessages(null);
                        }
                        if (time == 0) {//没有通话
                            mTv_call_time.setText("挂断结束");
                        } else {
                            mTv_call_time.setText("通话结束:" + TimeUtils.stringForTime(time));
                        }
                        if (!isClose) {
                            mCall_myvv.stopPlayback();
                            isClose = true;
                        }
                        if (isRecord) {
                            stopService(new Intent(CallUIActivity.this, RecordingService.class));
                        }
                        break;
                    case "STATE_DISCONNECTING"://拨打电话,自己挂断
                        if (!isUsed) {
                            mPhoneCallManager.disconnect();
                            mPhoneCallManager.destroy();
                            unregisterReceiver(mCallPhoneStateBr);
                            isUsed = true;
                        }
                        if (mHandler != null) {
                            mHandler.removeCallbacksAndMessages(null);
                        }
                        if (time == 0) {//没有通话
                            mTv_call_time.setText("正在挂断");
                        } else {
                            mTv_call_time.setText("通话结束:" + TimeUtils.stringForTime(time));
                        }
                        if (!isClose) {
                            mCall_myvv.stopPlayback();
                            isClose = true;
                        }
                        if (isRecord) {
                            stopService(new Intent(CallUIActivity.this, RecordingService.class));
                        }
                        break;
                }
            }
        }
    }
}
